<template>
  <!-- TODO: 判断一个对象是否为空: Object.keys(baseInfo).length !== 0 -->
  <div v-if="Object.keys(baseInfo).length" class="base-info">
    <!-- 标题 -->
    <div class="baseinfo-title">{{ baseInfo.title }}</div>
    <!-- 价格 -->
    <div class="baseinfo-price">
      <span class="new-price">{{ baseInfo.newPrice }}</span>
      <span class="old-price">{{ baseInfo.oldPrice }}</span>
      <!-- TODO: 判断此商品是否有打折优惠 -->
      <span
        v-if="baseInfo.discount"
        class="discount"
        :style="{
          'background-color': baseInfo.discountbgColor
            ? baseInfo.discountbgColor
            : '#ff5777',
        }"
        >{{ baseInfo.discount }}</span
      >
    </div>
    <!-- 销量/收藏/快递信息 -->
    <div class="baseinfo-other">
      <span>{{ baseInfo.columns[0] }}</span>
      <span>{{ baseInfo.columns[1] }}</span>
      <!-- 这个数据有点扯拐 无语 -->
      <span>{{ baseInfo.services[baseInfo.services.length - 1].name }}</span>
    </div>
    <!-- TODO: 服务项目(数量不确定, 需要遍历) -->
    <div class="baseinfo-service">
      <!-- TODO: 这里遍历的是索引, 这里length -1 是为了排除重复的数据 -->
      <span
        class="baseinfo-service-item"
        v-for="idx in baseInfo.services.length - 1"
        :key="idx"
      >
        <!-- 小图标 -->
        <!-- TODO: 关于这里为什么要 减1的问题: 因为遍历的数字是从1开始的,不是从0开始的, 不要被idx这个名字迷惑 -->
        <img
          :src="
            baseInfo.services[idx - 1].icon
              ? baseInfo.services[idx - 1].icon
              : baseInfo.services[baseInfo.services.length - 1].icon
          "
          alt=""
        />
        <!-- 文字 -->
        <span>{{ baseInfo.services[idx - 1].name }}</span>
      </span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    baseInfo: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {}
  },
  methods: {},
  components: {}
}
</script>

<style lang="less" scoped>
.base-info {
  padding: 15px 10px 0;
  border-bottom: 5px solid #f2f5f8;

  .baseinfo-title {
    color: #000;
    font-size: 18px;
  }

  .baseinfo-price {
    position: relative;
    margin: 15px 0;

    .new-price {
      font-size: 24px;
      color: var(--color-high-text);
    }

    .old-price {
      display: inline-block;
      margin: 0 5px;
      color: #9f9f9f;
      font-size: 14px;
      text-decoration: line-through;
    }

    .discount {
      padding: 2px 5px;
      border-radius: 10px;
      font-size: 14px;
      color: #fff;
      // 让这个元素上浮
      position: absolute;
      top: 3px;
    }
  }

  .baseinfo-other {
    display: flex;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 2px solid #f2f5f8;
    color: #9f9f9f;
    font-size: 14px;
  }

  .baseinfo-service {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    margin: 15px 0;

    .baseinfo-service-item {
      margin: 5px 0;
      color: #000;
      font-size: 14px;

      img {
        width: 14px;
        margin: -2px 1px 0 0;
        vertical-align: middle;
      }
    }
  }
}
</style>
